# --------------------------------------------------------------------------------
#  NoiseTube Web application
#  
#  Copyright (C) 2008-2010 SONY Computer Science Laboratory Paris
#  Portions contributed by Vrije Universiteit Brussel (BrusSense team), 2008-2011
# --------------------------------------------------------------------------------
#  This library is free software; you can redistribute it and/or modify it under
#  the terms of the GNU Lesser General Public License, version 2.1, as published
#  by the Free Software Foundation.
#  
#  This library is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
#  details.
#  
#  You should have received a copy of the GNU Lesser General Public License along
#  with this library; if not, write to:
#    Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor,
#    Boston, MA  02110-1301, USA.
#  
#  Full GNU LGPL v2.1 text: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
#  NoiseTube project source code repository: http://code.google.com/p/noisetube
# --------------------------------------------------------------------------------
#  More information:
#   - NoiseTube project website: http://www.noisetube.net
#   - Sony Computer Science Laboratory Paris: http://csl.sony.fr
#   - VUB BrusSense team: http://www.brussense.be
# --------------------------------------------------------------------------------
 
class Cartasur::HomepageController < Cartasur::ApplicationController
  include Geokit::Geocoders

  #where the cartasur dbs are stored
  CARTASUR_DB_BACKUPS_PATH = "public/dumps/cartasur/ntcartasur*"

  #caches_action :index, :layout=>false

  def index

  end

  def team

  end
  
  def publications
    render :layout => "layouts/application"
  end

  def api_overview

  end

  def download
    @dumps =  Dir.glob(Dir[CARTASUR_DB_BACKUPS_PATH])
    #todo
  end

  def contact
  end

  def help

  end

  # documents: for displaying uploaded documents
  def documents
    @docs = Cartasur::UserUpload.all
    @new_upload = Cartasur::UserUpload.new
    @new_upload.user = current_user

  end

  # uploaddoc: processes uploaded documents
  def uploaddoc
    user_id = params[:user]

    begin

    unless user_id.nil?
      user = Cartasur::User.find(user_id)
      #assign upload to him
      doc = Cartasur::UserUpload.new
      doc.user = user

      doc.document = params[:cartasur_user_upload][:document]
      doc.details = params[:cartasur_user_upload][:details]
      doc.comments = "Documents page"

      if doc.save
        flash[:notice] = t 'cartasur.uploads.uploadsuccess'
      else
        flash[:error] = t 'cartasur.uploads.uploadfailed'
      end

    end

    rescue Exception => e
      if e.message == 'file too large'
        flash[:error] = t 'cartasur.uploads.uploadtoolarge'
      else
        raise e
      end
    end

    redirect_to controller: :homepage, action: :documents
  end
  def removedoc
    if current_user && current_user.role == "admin"
      Cartasur::UserUpload.delete params[:doc] unless params[:doc].nil?
      flash[:notice] = t 'cartasur.uploads.uploaddeleted'
    else
      flash[:error] = "Could not delete the document"
    end
    redirect_to :controller => "cartasur/homepage", :action => "documents"
  end
  def downloaddoc
    unless params[:doc].nil?
      doc = Cartasur::UserUpload.find(params[:doc])
      send_file doc.document.to_s
    end
  end
   
end
